from models.model import *


class Laboratory(db.Model):
    __tablename__ = "laboratory"
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}
    laboratory_id = db.Column(db.Integer, primary_key=True, autoincrement=True)  
    laboratory_loc = db.Column(db.String(64), nullable=False) # 课程健身房
    laboratory_name = db.Column(db.String(32), nullable=False) # 课程名称
    laboratory_time = db.Column(db.String(128), nullable=False) # 时间
    laboratory_owner = db.Column(db.String(128), nullable=False) # 教练
    laboratory_left = db.Column(db.Integer, default=40)


def initial_lab():
    laboratories = [
        Laboratory(laboratory_loc='健身房1', laboratory_name='健身1', laboratory_time='2023-3-1 17:30:00', laboratory_owner='教练1', laboratory_left=50),
        Laboratory(laboratory_loc='健身房1', laboratory_name='健身2', laboratory_time='2023-3-2 17:30:00', laboratory_owner='教练2', laboratory_left=40),
        Laboratory(laboratory_loc='健身房1', laboratory_name='健身3', laboratory_time='2023-3-3 17:30:00', laboratory_owner='教练3', laboratory_left=30),
        Laboratory(laboratory_loc='健身房1', laboratory_name='健身4', laboratory_time='2023-3-4 17:30:00', laboratory_owner='教练4', laboratory_left=15),
        Laboratory(laboratory_loc='健身房1', laboratory_name='健身5', laboratory_time='2023-3-5 17:30:00', laboratory_owner='教练5', laboratory_left=40),
        Laboratory(laboratory_loc='健身房1', laboratory_name='健身6', laboratory_time='2023-3-6 17:30:00', laboratory_owner='教练6', laboratory_left=20),
        Laboratory(laboratory_loc='健身房1', laboratory_name='健身7', laboratory_time='2023-3-7 17:30:00', laboratory_owner='教练7', laboratory_left=25),
        Laboratory(laboratory_loc='健身房1', laboratory_name='健身8', laboratory_time='2023-3-8 17:30:00', laboratory_owner='教练8', laboratory_left=30),
        Laboratory(laboratory_loc='健身房1', laboratory_name='健身9', laboratory_time='2023-3-9 17:30:00', laboratory_owner='教练9', laboratory_left=10),
        Laboratory(laboratory_loc='健身房2', laboratory_name='健身10',laboratory_time='2023-3-10 17:30:00', laboratory_owner='教练10', laboratory_left=5),
        Laboratory(laboratory_loc='健身房2', laboratory_name='健身11',laboratory_time='2023-3-11 17:30:00', laboratory_owner='教练11', laboratory_left=2),
        Laboratory(laboratory_loc='健身房3', laboratory_name='健身12',laboratory_time='2023-3-12 17:30:00', laboratory_owner='教练12', laboratory_left=6)
    ]
    db.session.add_all(laboratories)

    db.session.commit()


def change_left(laboratory_id, if_appointment):
    laboratory = db.session.query(Laboratory).filter_by(laboratory_id=laboratory_id).first()
    if if_appointment:
        if laboratory.laboratory_left == 0:
            return False
        laboratory.laboratory_left -= 1
    else:
        laboratory.laboratory_left += 1

    db.session.commit()


def show_all_lab(name):
    left_all = db.session.query(Laboratory.laboratory_left).filter(Laboratory.laboratory_name.like(name+"%")).all()
    numbers = 0
    for i in left_all:
        numbers += i[0]
    # print(numbers)
    return numbers

